package vno.fux.utils
{
	import fl.controls.Label;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.ProgressEvent;
	import flash.text.TextFieldAutoSize;
	
	import vno.fux.GlobalEventScheduler;
	import vno.fux.GlobalVar;
	public class MissionManager extends EventDispatcher
	{
		private static var _currentId:uint = 0;
		private var _id:uint;
		private var _missionId:uint;
		private var _missionQueue:Vector.<Mission>;
		private var _currentMission:Mission;
		private var _isRunning:Boolean;
		private var _messageLabel:Label;
		public function MissionManager()
		{
			_id = MissionManager._currentId++;
			_missionId = 0;
			_missionQueue = new Vector.<Mission>;
			_isRunning = false;
//			_messageLabel = new Label;
//			_messageLabel.mouseEnabled = false;
//			_messageLabel.text = "Message-\n";
//			_messageLabel.y = GlobalVar.stage.stageHeight - _messageLabel.height;
//			_messageLabel.autoSize = TextFieldAutoSize.LEFT;
			//GlobalEventScheduler.getInstance().addResizeListener(resizeHandler);
			//if(_id==0) GlobalVar.stage.addChild(_messageLabel);//fux
		}
		
		private function resizeHandler(event:Event):void
		{
			_messageLabel.y = GlobalVar.stage.stageHeight - _messageLabel.textField.height;
		}
		public function addMission(mission:Mission):void
		{
			mission.id = _missionId++;
			_missionQueue.push(mission);
		}
		public function run():void
		{
			_currentMission = _missionQueue.shift();
			if(_currentMission)
			{
				runMission(_currentMission);
				_isRunning = true;	
			}
			else
			{
				dispatchEvent(new Event(Event.COMPLETE+_id));
				_isRunning = false;	
			}
		}
		private function runMission(mission:Mission):void
		{
			var closure:Function = mission.closure;
			var params:Object = mission.params;
			var listener:EventDispatcher = mission.listener;
			_currentMission = mission;
			_isRunning = true;
			if(listener)
			{
				listener.addEventListener(mission.completeEvent, missionCompleteHandler);
				if(mission.progressEvent) listener.addEventListener(mission.progressEvent, missionProgressHandler);
				callClosure(closure, params);
			}
			else
			{
				callClosure(closure, params);
				missionCompleteHandler(null);
			}

		}
		private function callClosure(closure_:Function, params_:Object):void
		{
			if(params_!=null)
			{
				closure_(params_);
			}
			else
			{
				closure_();
			}
		}
		
		protected function missionProgressHandler(event:ProgressEvent):void
		{
			var current:Number = _currentMission.id+(event.bytesTotal==0?0:event.bytesLoaded/event.bytesTotal);
			var progressEvent:ProgressEvent = new ProgressEvent(ProgressEvent.PROGRESS, false, false, current, _missionId);
			dispatchEvent(progressEvent);
		}
		
		protected function missionCompleteHandler(event:Event):void
		{
			trace(this+_currentMission);
//			var strTmp:String = _messageLabel.text;
//			strTmp += this+_currentMission+'\n';
//			_messageLabel.text = strTmp;
//			_messageLabel.y = GlobalVar.stage.stageHeight - _messageLabel.textField.height;
			if(_currentMission.listener)
			{
				_currentMission.listener.removeEventListener(_currentMission.completeEvent, missionCompleteHandler);
				if(_currentMission.progressEvent) _currentMission.listener.removeEventListener(_currentMission.progressEvent, missionProgressHandler);
			}
			dispatchEvent(new Event(Event.COMPLETE+_id+'-'+_currentMission.id));
			dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, _currentMission.id+1, _missionId));
			var nextMission:Mission = _missionQueue.shift();
			if(nextMission)
			{
				runMission(nextMission);
			}
			else
			{
				dispatchEvent(new Event(Event.COMPLETE));
				_isRunning = false;	
			}
		}

		public function get id():uint
		{
			return _id;
		}
		
		override public function toString():String
		{
			return "MissionManager[id="+_id+"]";
		}
		
	}
}